package com.watchmen.mini.common.config;

import com.watchmen.mini.modules.sys.interceptor.MobileInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	/**
	 * 跨域支持
	 */
	@Bean
	public WebMvcConfigurer corsConfigurer() {
		return new WebMvcConfigurer() {
			public void addCorsMappings(CorsRegistry registry) {
				registry.addMapping("/**")
						.allowedMethods("*")
						.allowedHeaders("*")
						.allowedOrigins("*")
						.allowCredentials(true)
						.maxAge(18000L);
			}
		};
	}


	/**
	 * 自定义拦截器地址
	 * @return
	 */
	@Bean
	public HandlerInterceptor MobileInterceptor(){
		return new MobileInterceptor();
	}

	/**
	 * 添加拦截路径
	 * @param registry
	 */
	public void addInterceptors(InterceptorRegistry registry) {
		String url = "/**";
		//添加排除排除URL校验路径
		registry.addInterceptor(MobileInterceptor()).addPathPatterns(url).excludePathPatterns(
				"/error",
				"/api/test/*",
                "/doc.html",
                "/v2/api-docs",
                "/swagger-resources",
                "/swagger-resources/configuration/ui"
		);
	}

}
